package com.bristua.user.biz;
import com.bristua.common.bean.PageBean;
import com.bristua.user.dao.TUserBankMapper;
import com.bristua.user.dao.TUserCashTakeMapper;
import com.bristua.user.model.TUserBank;
import com.bristua.user.model.TUserCashTake;
import com.bristua.user.model.condition.QueryUserTakeCashParams;
import com.bristua.user.model.condition.TUserBankExample;
import com.bristua.user.model.condition.TUserCashTakeExample;
import com.bristua.user.model.vo.QueryUserTakeCashVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/4/2.
 */
@RestController
public class QueryUserTakeCashBiz {
    @Resource
    private TUserCashTakeMapper tUserCashTakeMapper;

    @Resource
    private TUserBankMapper tUserBankMapper;

    public PageBean queryUserTakeCash(QueryUserTakeCashParams queryUserTakeCashParams){
        TUserCashTakeExample tUserCashTakeExample = new TUserCashTakeExample();
        tUserCashTakeExample.setOrderByClause("create_time desc");
        TUserCashTakeExample.Criteria criteria = tUserCashTakeExample.createCriteria();

        if(!StringUtils.isEmpty(queryUserTakeCashParams.getStartTime())){
            criteria.andCreateTimeGreaterThan(queryUserTakeCashParams.getStartTime()+" 00:00:00");
        }

        if(!StringUtils.isEmpty(queryUserTakeCashParams.getEndTime())){
            criteria.andCreateTimeLessThan(queryUserTakeCashParams.getEndTime()+" 23:59:59");
        }

        Page<TUserCashTake> objects = PageHelper.startPage(queryUserTakeCashParams.getPageNo(),queryUserTakeCashParams.getPageSize());
        tUserCashTakeMapper.selectByExample(tUserCashTakeExample);
        PageBean data = new PageBean<>(objects);
        if(data.getList()!=null && data.getList().size()>0){
            TUserBankExample tUserBankExample = new TUserBankExample();
            List<QueryUserTakeCashVO> queryUserTakeCashVOS = new ArrayList<>();
            for(int i=0;i<data.getList().size();i++){
                TUserCashTake tUserCashTake = (TUserCashTake) data.getList().get(i);
                QueryUserTakeCashVO queryUserTakeCashVO = new QueryUserTakeCashVO();
                BeanUtils.copyProperties(tUserCashTake,queryUserTakeCashVO);
                tUserBankExample.createCriteria().andUserBankIdEqualTo(tUserCashTake.getBankId());
                List<TUserBank> tUserBanks = tUserBankMapper.selectByExample(tUserBankExample);
                if(tUserBanks!=null && tUserBanks.size()>0){
                    queryUserTakeCashVO.setBankNo(tUserBanks.get(0).getBankNo());
                }
                queryUserTakeCashVOS.add(queryUserTakeCashVO);
//                tDevice.setIsOnline(DeviceCode.getValueByCode(Integer.valueOf(tDevice.getIsOnline())));
            }
            data.getList().clear();
            data.getList().addAll(queryUserTakeCashVOS);
        }
        return data;

    }
}
